import os
import sys

from PySide6.QtCore import Qt, QSettings, QByteArray
from PySide6.QtGui import QIcon, QGuiApplication, QAction
from PySide6.QtWidgets import (QApplication, QMainWindow, QMenuBar, QMessageBox,
                             QTextEdit, QToolBar, QVBoxLayout, QWidget)

from .playerwidget import PlayerWidget
from . import images_rc
from .utils import R


class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.resize(800, 600)
        screen = QGuiApplication.primaryScreen().availableGeometry()
        size = self.geometry()
        self.move(int((screen.width()-size.width())/2), 60)
        self.setWindowTitle("x01 - " + R.AppName)

        self.play_widget = PlayerWidget(self)
        self.setCentralWidget(self.play_widget)

        # self.centralWidget = QWidget(self)
        # self.setCentralWidget(self.centralWidget)

        # self.verticalLayout = QVBoxLayout(self.centralWidget)
        # self.textEdit = QTextEdit(self.centralWidget)
        # self.verticalLayout.addWidget(self.textEdit)

        # self.InitSettings()
        # self.InitActions()
        # self.InitMenu()
        # self.InitToolBar()

        self.filename = ''

    def closeEvent(self, e):
        ''' override closeEvent for settings '''
        settings = QSettings('x01', R.AppName)
        settings.setValue('filename', self.filename)
        settings.setValue("MainWindow/Geometry", self.saveGeometry())
        settings.setValue("MainWindow/State", self.saveState())

    def InitSettings(self):
        settings = QSettings('x01', R.AppName)
        if settings.value('filename'):
            self.filename = settings.value('filename')
        if settings.value("MainWindow/Geometry"):
            self.restoreGeometry(
                QByteArray(settings.value("MainWindow/Geometry")))
        if settings.value("MainWindow/State"):
            self.restoreState(QByteArray(settings.value("MainWindow/State")))

    def InitActions(self):
        self.fileNewAction = QAction(QIcon(':images/new_file.gif'),
            '&New', self, shortcut='Ctrl+N', triggered=self.FileNew)
        self.fileOpenAction = QAction(QIcon(':images/open_file.gif'),
            '&Open', self, triggered=self.FileOpen, shortcut='Ctrl+O')
        self.fileSaveAction = QAction(QIcon(':images/save.gif'),
            "&Save", self, triggered=self.FileSave, shortcut='Ctrl+S'
        )
        self.filePreviewAction = QAction(QIcon(':images/preview.gif'),
            'P&review', self, triggered=self.FilePreview, shortcut='Ctrl+R')
        self.filePrintAction = QAction(QIcon(':images/print.png'),
            '&Print', self, triggered=self.FilePrint, shortcut='Ctrl+P')

        self.fileQuitAction = QAction(QIcon(':images/quit.png'),
            "&Quit", self, triggered=self.close, shortcut="Ctrl+Q"
        )
        self.fileActions = [self.fileNewAction, self.fileOpenAction, self.fileSaveAction, None,
                            self.filePreviewAction, self.filePrintAction, None,
                            self.fileQuitAction]


        self.editUndoAction = QAction(QIcon(':images/undo.gif'),
            "&Undo", self, triggered=self.EditUndo
        )
        self.editRedoAction = QAction(QIcon(':images/redo.gif'),
            "&Redo", self, triggered=self.EditRedo
        )
        self.editCopyAction = QAction(QIcon(':images/copy.gif'),
            "&Copy", self, triggered=self.EditCopy, shortcut='Ctrl+C'
        )
        self.editCutAction = QAction(QIcon(':images/cut.gif'),
            "C&ut", self,  triggered=self.EditCut, shortcut='Ctrl+X'
        )
        self.editPasteAction = QAction(QIcon(':images/paste.gif'),
            "&Paste", self,  triggered=self.EditPaste, shortcut='Ctrl+V'
        )
        self.editActions = [self.editUndoAction, self.editRedoAction, None,
                            self.editCopyAction, self.editCutAction, self.editPasteAction]

        self.helpAction = QAction("&Help", triggered=self.Help)
        self.aboutAction = QAction("&About", triggered=self.About)
        self.helpActions = [self.helpAction, self.aboutAction]

    def InitToolBar(self):
        self.fileBar = QToolBar(self.tr('File toolbar'), self)
        self.addToolBar(Qt.ToolBarArea.TopToolBarArea, self.fileBar)
        R.AddActions(self.fileBar, self.fileActions[:-1])

        self.editBar = QToolBar(self.tr('Edit toolbar'), self)
        self.addToolBar(Qt.ToolBarArea.TopToolBarArea, self.editBar)
        R.AddActions(self.editBar, self.editActions)

    def InitMenu(self):
        self.menuBar = QMenuBar(self)
        self.setMenuBar(self.menuBar)

        self.menuFile = self.menuBar.addMenu(self.tr('File'))
        R.AddActions(self.menuFile, self.fileActions)

        self.menuEdit = self.menuBar.addMenu(self.tr('Edit'))
        R.AddActions(self.menuEdit, self.editActions)

        self.menuHelp = self.menuBar.addMenu(self.tr('Help'))
        R.AddActions(self.menuHelp, self.helpActions)


    def FileNew(self):pass
    def FileOpen(self):pass
    def FileSave(self): pass
    def FilePreview(self): pass
    def FilePrint(self): pass

    def EditUndo(self): pass
    def EditRedo(self): pass
    def EditCopy(self): pass
    def EditCut(self): pass
    def EditPaste(self): pass

    def Help(self): 
        QMessageBox.about(
            self, R.AppName, '略'
        )

    def About(self):
        QMessageBox.about(
            self, R.AppName,
            f'{R.AppName} 版本 {R.AppVersion}\nCopyright 2022 by x01. All rights reserved.\n版权属于黄雄(x01)，所有权利保留。'
        )


def run():
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec())


if __name__ == '__main__':
    run()  # 包内不可运行
